package org.toycode.logindemo.service;

import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.toycode.logindemo.entity.Article;
import org.toycode.logindemo.entity.Task;

/**
 * Mocks some data for testing.
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.0, Aug 31, 2009
 */
final public class ServerMockDataGenerator {

    /**
     * entity manager.
     */
    @PersistenceContext(name = "loginDemoPU")
    private EntityManager em;
    private static ServerMockDataGenerator instance;

    private ServerMockDataGenerator() {
    }

    private synchronized ServerMockDataGenerator getInstance() {
        if (instance == null) {
            instance = new ServerMockDataGenerator();
        }

        return instance;
    }

    public void mockData() {
        em.getTransaction().begin();

        for (int i = 0; i < 10; i++) {
            final Article article = new Article();
            article.setAuthor("author " + i);
            article.setPublishDate(new Date());
            em.persist(article);

            final Task task = new Task();
            task.setOwner("owner " + i);
            task.setDescription("description " + i);
            task.setProgressDescription("progress description " + i);
            em.persist(task);
        }

        em.getTransaction().commit();
    }
}
